<?php
namespace Api\Controller;

/**
 * 新闻资讯
 * @author liubo <liubo@ilongyuan.com.cn>
 */
class NewsController extends PublicController 
{


    /**
     * 资讯列表
     * @author liubo <liubo@ilongyuan.com.cn>
     */
    public function newslist()
    {
        $p = I('p', 1, 'intval');
        $limit = I('limit', 10, 'intval');

        $games = I('games', '', 'trim');
        $games = $games ? explode(',', $games) : [];
        $games = array_map('intval', $games);

        $query = ['type' => 0];
        if (!empty($games)) {
            $query['game_id'] = ['$in' => $games];
        }

        $nEvent = event('news');
        $newslist = $nEvent->lists($query, $p, $limit);

        $result['list'] = $newslist;

        // 第一页包含热门、banner数据
        if ($p == 1) {
            $query['is_hot'] = 1;
            $hotlist = $nEvent->lists($query);
            $result['hot'] = $hotlist;

            $code = 'newsbanner';
            $bannerlist = event('Ad')->getAdsByCode($code);
            foreach ($bannerlist as &$value) {
                uri_format($value['image']);
                $value = [
                    'id' => $value['id'],
                    'link' => $value['link'],
                    'name' => $value['name'],
                    'image' => $value['image'],
                ];
            }
            $result['banner'] = array_values($bannerlist);
        }
        $this->success('', $result);
    }

    /**
     * 公告列表
     * @author liubo <liubo@ilongyuan.com.cn>
     */
    public function noticelist()
    {
        $p = I('p', 1, 'intval');
        $limit = I('limit', 10, 'intval');

        $list = event('news')->lists(['type' => 1], $p, $limit);
        $this->success('', $list);
    }

    public function detail($id) {
        $nEvent = event('news');
        $news = $nEvent->detail(intval($id));
        if (empty($news)) {
            $this->error(self::$API_ERROR['PARAM'], '参数错误');
        }
        $nEvent->setReadCount($id);
        $this->success('', $news);
    }

    public function comment_add()
    {
        $this->checkLogin();
        $news_id = I('id', 0, 'intval');
        $content = I('comment', '', 'trim');
        if (!$news_id || !$content) {
            $this->error(self::$API_ERROR['PARAM'], '参数错误');
        }
        $res = event('NewsComments')->add($news_id, UID, $content);

        if (empty($res)) {
            $this->error(self::$API_ERROR['COMMENT'], '评论失败');
        }
        $this->success('评论成功', ['content' => $content]);
    }

    public function comments()
    {
        $news_id = I('id', 0, 'intval');
        if (!$news_id) {
            $this->error(self::$API_ERROR['PARAM'], '参数错误');
        }
        $p = I('p', 1, 'intval');
        $limit = I('limit', 10, 'intval');

        $total = 0;
        $list = event('NewsComments')->get($news_id, true, $p, $limit, $total);
        $this->list_success($list, $total);
    }
}